package com.cg.controller;

import com.cg.dto.Result;
import com.cg.dto.ResultEnum;
import com.cg.dto.ResultUtils;
import com.cg.entity.Patient;
import com.cg.service.NumService;
import com.cg.service.PatientService;
import com.cg.utils.DateUtils;
import com.cg.vo.PatientListVo;
import com.cg.vo.PatientVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * (Patient)表控制层
 *
 * @author chengong
 * @since 2023-12-31 13:47:36
 */
@RestController
@RequestMapping("/patient")
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)//代理使用默认的
public class PatientController {
    /**
     * 服务对象
     */
    @Resource
    private PatientService patientService;

    private final Logger logger = LoggerFactory.getLogger(PatientController.class);

    @Resource
    private NumService numService;

    /**
     * 就诊人管理列表
     */
    @GetMapping("/patientListByPatientUserId")
    public Result<List<PatientListVo>> patientListByPhone(@RequestParam("patientUserId") Integer patientUserId) throws ParseException {
        if(patientUserId == 0){
            return ResultUtils.returnResult(ResultEnum.TOKEN_PATIENT_NULL);
        }
        List<PatientListVo> patientListVos = patientService.patientListByPhone(patientUserId);
        for (PatientListVo patientListVo : patientListVos) {
            //获取出生年月
            String birthday = patientListVo.getBirthday();
            logger.info("生日:{}"+birthday);
            //获取当前年月
            Integer age = DateUtils.getAgeByBirthday(birthday);
            logger.info("年龄"+age);
            patientListVo.setAge(age);
        }
        return ResultUtils.returnDataSuccess(patientListVos);
    }


    /**
     * 查询就诊人详情
     */
    @GetMapping("/queryPatientByCardNum")
    public Result<PatientVo> queryPatientByCardNum(@RequestParam("cardNum") String cardNum){
        return ResultUtils.returnDataSuccess(patientService.queryPatientByCardNum(cardNum));
    }


    /**
     * 添加就诊人
     */
    @PostMapping("/createPatient")
    public Result createPatient(@RequestBody Patient patient){
        Integer count = patientService.createPatient(patient);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.COMMON_EXCEPTION);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 生成条形码
     */
    @GetMapping("/barCode")
    public Result<String> barCode(@RequestParam("cardNum") String cardNum){
        return ResultUtils.returnDataSuccess(numService.createBarCode(cardNum));
    }

    /**
     * 生成二维码
     */
    @GetMapping("/code")
    public Result<String> code(@RequestParam("cardNum") String cardNum){
        return ResultUtils.returnDataSuccess(numService.createCode(cardNum));
    }


    /**
     * 根据token获取默认的就诊人信息
     */
    @PostMapping("/getPatientByToken")
    public Result<Patient> getPatientByToken(@RequestParam("userId") Integer userId){
        return patientService.getPatientByToken(userId);
    }


    /**
     * 根据登录用户的ID查询就诊人数量
     */
    @GetMapping("/queryCountByPatientUserId")
    public Result queryCountByPatientUserId(@RequestParam("patientUserId") Integer patientUserId){
        return ResultUtils.returnDataSuccess(patientService.queryCountByPatientUserId(patientUserId));
    }

}

